package cn.fl.customer.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstCorpHisService;
import cn.fl.customer.vo.CstCorpHisVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;
import fl.customer.facade.customer.condition.CstCorpHisQueryCondition;

/**
 * @Description: 法人公司历史信息表 控制层
 */
@Controller
@RequestMapping("/customer/cstcorphis")
public class CstCorpHisController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CstCorpHisController.class);

	/**
	 * CstCorpHis 服务层BEAN
	 */
	@Autowired
	private ICstCorpHisService cstCorpHisService;

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String cstId = request.getParameter("cstId");
		logger.info("加载CstCorpHis Modify页面.");
		ModelAndView view = new ModelAndView("customer/cstcorphis/cstCorpHisAdd");
		view.addObject("id", id);
		view.addObject("cstId", cstId);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面!ID:"+id, CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到详细页面
	 */
	@RequestMapping(value = "/toDetailPage.do", method = { RequestMethod.GET })
	public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		logger.info("加载CstCorpHis 详情页面.");
		ModelAndView view = new ModelAndView("customer/cstcorphis/cstCorpHisDetail");
		view.addObject("id", id);
		BaseLogUtils.info(logger, "toDetailPage", "跳转到详情页面!ID:"+id, CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String cstId = request.getParameter("cstId");
		CstCorpHisQueryCondition qc = new CstCorpHisQueryCondition();
		List<CstCorpHisVO> list = null;
		if (StringUtils.isNotEmpty(cstId)) {
			qc.setCstId(Long.valueOf(cstId));
			list = this.cstCorpHisService.searchList(qc);
		}else{
			list = new ArrayList<>();
		}
		ListPageVO<CstCorpHisVO> listPage = new ListPageVO<CstCorpHisVO>(list, 0);
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.cstCorpHisService.removeById(Long.valueOf(id));
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		
		try {
			CstCorpHisVO vo = this.cstCorpHisService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
			}
			result.setData(vo);
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		try {
			CstCorpHisVO entity = this.getRequestParms(request);
			entity.setIsDel(BaseConstant.IS_YESNO_NO);
			entity.setCrtTm(new Date());
			entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			Long id = this.cstCorpHisService.add(entity);
			result.setData(id);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
		}

		return result;
	}
	

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			CstCorpHisVO entity = this.getRequestParms(request);
			entity.setId(Long.valueOf(id));
			entity.setMdfTm(new Date());
			entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			boolean success = this.cstCorpHisService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_UPDATE_FAIL);
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
		}
		return result;
	}
	
	/**
	 * 获取正常请求参数-属于新增 修改的属性需要在自己方法中设置
	 * @throws BaseException
	 */
	private CstCorpHisVO getRequestParms(HttpServletRequest request) throws BaseException {
		String prjNm = request.getParameter("prjNm");
		String startDt = request.getParameter("startDt");
		String endDt = request.getParameter("endDt");
		String prjDesc = request.getParameter("prjDesc");
		String cstId = request.getParameter("cstId");
		
		//验证
		if(StringUtils.isEmpty(prjNm)){
			BaseLogUtils.info(logger, "add", "项目名称不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "项目名称不能为空!");
		}
		
		if(StringUtils.isEmpty(startDt)){
			BaseLogUtils.info(logger, "add", "开始时间不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "开始时间不能为空!");
		}
		
		if(StringUtils.isEmpty(endDt)){
			BaseLogUtils.info(logger, "add", "结束时间不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "结束时间不能为空!");
		}
		
		Date startDate = null;
		Date endDate = null;
		try {
			startDate = DateUtils.formatStringToDate(startDt, DateUtils.YYYY_MM_DD);
			endDate = DateUtils.formatStringToDate(endDt, DateUtils.YYYY_MM_DD);
		} catch (Exception e) {
			BaseLogUtils.info(logger, "add", "日期解析错误!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "日期解析错误!");
		}
		
		Date currentDate = new Date();
		if(currentDate.before(startDate)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "开始时间不能大于当前时间!");
		}
		
		if(endDate.before(startDate)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "开始时间不能大于结束时间!");
		}
		
		if(StringUtils.isEmpty(cstId)){
			BaseLogUtils.info(logger, "add", "客户不能为空!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "客户不能为空!");
		}
		
		if(StringUtils.isNotEmpty(prjDesc) && prjDesc.length() > 250){
			BaseLogUtils.info(logger, "add", "项目描述只能输入250个字符!");
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "项目描述只能输入250个字符!");
		}
		
		CstCorpHisVO entity = new CstCorpHisVO();
		entity.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
		entity.setCstId(Long.valueOf(cstId));
		entity.setStartDt(startDate);
		entity.setEndDt(endDate);
		entity.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		entity.setPrjDesc(prjDesc);
		entity.setPrjNm(prjNm);
		return entity;
	}
}

